package com.dy.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.dy.utils.CurrentHolder;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * ClassName:MpMeta
 * Package:com.dy.common
 * Description:
 *
 * @Date:2022/8/3 23:20
 * @Author:dy
 */
@Component
public class MpMetaObjectHandler implements MetaObjectHandler {

    //@TableField(fill = FieldFill.INSERT)
    //为添加该 注解的 实体类属性字段，在进行数据库操作前，初始化数据.
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", CurrentHolder.getCurrent());
        metaObject.setValue("updateUser", CurrentHolder.getCurrent());
    }

    //@TableField(fill = FieldFill.INSERT_UPDATE)
    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", CurrentHolder.getCurrent());
    }
}
